<template>
	<view class="page">
		<cus-tabs v-if="typeData.length>0" class="tab-box" :list="typeData" :showBar="false" :current="typeIndex"
			activeColor="#d01919" @change="typeChangeHandle">
		</cus-tabs>
		<cus-tabs class="tab-box" v-if="minorData.length>0" :list="minorData" :showBar="false" :current="minorIndex"
			activeColor="#d01919" @change="minorChangeHandle"></cus-tabs>

		<scroll-view scroll-y="true" style="position: absolute;" :style="{ height:'calc(100% - '+tabsH+'px)'}"
			@scrolltolower="scrolltolower">
			<view class="novel" v-for="(item,index) in items" :key="item.id" @tap="novelTap(item)">
				<view class="content">
					<image class="default-cover cover" :data-cover="item.cover?'':'暂无封面'" :src="item.cover"
						mode="scaleToFill"></image>
					<view class="right">
						<view class="title">{{item.title}}</view>
						<view class="author-cat">
							{{item.author}}
							<text v-if="item.cat" class="split">·</text>
							{{item.cat}}
							<text v-if="item.wordCount" class="split">·</text>
							{{item.wordCount}}
						</view>
						<view v-if="item.lastChapter" class="desc">{{item.lastChapter}}</view>
						<view v-if="item.shortIntro" class="desc">{{item.shortIntro}}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapGetters
	}
	from 'vuex'
	export default {
		data() {
			return {
				curPage: 1,
				pageLimit: 20,
				loadMoreStatus: 0, //加载更多 0加载前，1加载中，2没有更多了
				items: [],
				typeIndex: 0,
				minorIndex: 0,
				typeData: [],
				minorData: [],
				option: {},
				tabsH: 0,
			}
		},
		computed: {
			...mapGetters({
				'curSource': 'get_curSource'
			}),
		},
		onLoad(option) {
			this.typeData = this.curSource.typeData || []

			uni.setNavigationBarTitle({
				title: option.genderName
			})
			this.option = option
			this.getBookList('refresh')
		},
		methods: {
			getCategoryLevel(option) {
				return new Promise(async (resolve, reject) => {
					let params = option.href
					let res = await this.$store.dispatch('getCategoryLevel', params)
					if (res) {
						resolve(res)
					}
				})
			},
			async getBookList(type) {
				let params = ''
				switch (this.curSource.value) {
					case 'zhuishushenqi':
						if (this.minorData.length === 0) {
							this.minorData = await this.getCategoryLevel(this.option)
							this.tabsH = 90
						}
						params = {
							gender: this.option.gender,
							type: this.typeData[this.typeIndex].value,
							query: this.option.major,
							cat: this.minorData[this.minorIndex].replace('全部', ''),
							start: (this.curPage - 1) * this.pageLimit,
							limit: this.pageLimit,
							packageName: '',
							channelName: ''
						}
						break
					case 'xbiquge':
						params = this.option.href + '_' + this.curPage + '.html'
						break
				}
				let res = await this.$store.dispatch('getBookList', params)
				if (res) {
					if (type === 'refresh') {
						this.items = res.books
					}
					if (type === 'add') {
						this.items.push(...res.books)
					}
					this.loadMoreStatus = res.pages > this.curPage ? 0 : 2
				}
			},
			typeChangeHandle(e) {
				this.typeIndex = e
				this.getBookList('refresh')
			},
			minorChangeHandle(e) {
				this.minorIndex = e
				this.getBookList('refresh')
			},
			async novelTap(item) {
				uni.navigateTo({
					url: '/pages/public/bookDetail?id=' + item._id
				})
			},
			scrolltolower() {
				if (this.loadMoreStatus === 2) {
					return;
				}
				this.loadMoreStatus = 1;
				this.curPage += 1
				this.getBookList('add')
			},
		}
	}
</script>
<style lang="scss">
	.tab-box {
		padding: 0 30rpx;
		border-bottom: 1px solid #ddd;
	}

	.novel {
		padding: 30rpx;

		.content {
			display: flex;
			justify-content: flex-start;

			.cover {
				width: 120rpx;
				height: 160rpx;
			}

			.right {
				width: 570rpx;
				padding-left: 30rpx;
				box-sizing: border-box;

				.title {
					// font-size: 28rpx;
					font-weight: bold;
				}

				.author-cat {
					color: #999;
					font-size: 26rpx;
					height: 40rpx;
					line-height: 40rpx;

					.split {
						padding: 0 16rpx;
					}
				}

				.desc {
					color: #999;
					font-size: 26rpx;
					height: 40rpx;
					line-height: 40rpx;

					overflow: hidden;
					text-overflow: ellipsis;
					word-break: break-all;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}
			}
		}
	}

	.novel+.novel {
		margin-top: 10rpx;
		border-top: 1px solid #f8f8f8;
	}
</style>
